package com.test.qnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.activity.DetailsActivity;
import com.test.qnews.adapter.CommonAdapter;
import com.test.qnews.beans.GsonBean;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.custom.RefreshLayout;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class EntertainmentFragment extends Fragment{
    private Context context;
    private ListView  listView;
    private List<NetDataBean> datas;
    private CommonAdapter commonAdapter;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/179223212?client_os=android&client_version=2.1.0.1&bid=800&m_ch=004&city=0411&sn=ade627ae9c8b3165f8beab83650296fd&content_cnt=20&recoid=&ftime=0&method=new&count=20";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_recommend);
        data();


        final RefreshLayout myRefreshListView = (RefreshLayout) view.findViewById(R.id.swipe_layout);
        myRefreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myRefreshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data();
                        myRefreshListView.setRefreshing(false);
                    }
                },2000);
            }
        });


        myRefreshListView.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                myRefreshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data();
                        myRefreshListView.setLoading(false);
                    }
                },2000);
            }
        });
    }

    private void data() {

        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                datas = new ArrayList<>();
                GsonBean gsonBean = gson.fromJson(response, GsonBean.class);
if (gsonBean.getData().getArticle().size() > 0) {
    for (int i = 0; i < gsonBean.getData().getArticle().size(); i++) {
        NetDataBean netDataBean = new NetDataBean();
        netDataBean.setSource_name(gsonBean.getData().getArticle().get(i).getSource_name());
        netDataBean.setContent(gsonBean.getData().getArticle().get(i).getContent());

        netDataBean.setTitle(gsonBean.getData().getArticle().get(i).getTitle());
//                    netDataBean.setTime(gsonBean.getData().getArticle().get(i).getComment_cnt());
        netDataBean.setItem_type(gsonBean.getData().getArticle().get(i).getItem_type());
        for (int j = 0; j < gsonBean.getData().getArticle().get(i).getImage().size(); j++) {
            netDataBean.setImageUrl(gsonBean.getData().getArticle().get(i).getImage().get(0).getUrl());
        }
        datas.add(netDataBean);
    }
}


                commonAdapter = new CommonAdapter(datas, context);
                listView.setAdapter(commonAdapter);


                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), DetailsActivity.class);
                        intent.putExtra("pos", position);
                        intent.putExtra("data", (Serializable) datas);
                        startActivity(intent);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


    }
}
